package com.ysd.resourceusestatistics.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ysd.resourceusestatistics.dao.IMemberDao;
import com.ysd.resourceusestatistics.dto.MemberDTO;
import com.ysd.resourceusestatistics.model.Member;
import com.ysd.resourceusestatistics.util.CardUtil;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * MemberServiceImpl
 */
@Service
public class MemberServiceImpl implements IMemberService {
@Resource
    private IMemberDao memberDao;

    @Override
    public Page<Member> findPage(MemberDTO dto) {
    

        Sort sort = Sort.by(Sort.Order.asc("id"));
        Pageable page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort);
        return memberDao.findAll(getWhereClause(dto), page);
    }

    private Specification<Member> getWhereClause(final MemberDTO dto) {

        return new Specification<Member>() {


            /**
             *
             */
            private static final long serialVersionUID = 8312028476150657319L;

            @Override
            public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
              
                Predicate p = criteriaBuilder.conjunction();
                List<Expression<Boolean>> exList = p.getExpressions();
                if (dto.getCardNo()!=null && !"".equals(dto.getCardNo())) {
                    exList.add(criteriaBuilder.equal(root.<String>get("cardNo"), dto.getCardNo()));
                }              if (dto.getName()!=null && !"".equals(dto.getName())) {
                    exList.add(criteriaBuilder.like(root.<String>get("name"),"%"+ dto.getName()+"%"));
                }              if (dto.getPhone()!=null && !"".equals(dto.getPhone())) {
                    exList.add(criteriaBuilder.equal(root.<String>get("phone"), dto.getPhone()));
                }if (dto.getStartDate()!=null) {
                    exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getStartDate()));
                }if (dto.getEndDate()!=null) {
                    exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"), dto.getEndDate()));
                }if (dto.getStatus()!=null) {
                    exList.add(criteriaBuilder.equal(root.<Integer>get("status"),dto.getStatus()));
                }if (dto.getCategory()!=null&&dto.getCategory()==0) {
                    if (dto.getDepartment()!=null&&!"".equals(dto.getDepartment())) {
                        exList.add(criteriaBuilder.equal(root.<String>get("department"),dto.getDepartment()));
                    }if (dto.getProfession()!=null&&!"".equals(dto.getProfession())) {
                        exList.add(criteriaBuilder.equal(root.<String>get("profession"),dto.getProfession()));
                    }if (dto.getGrade()!=null&&!"".equals(dto.getGrade())) {
                        exList.add(criteriaBuilder.equal(root.<String>get("grade"),dto.getGrade()));
                    }


                }
                
                return p;
            }
        
        
        };
    }

    @Override
    public Member saveMember(Member m) {
       
        m.setCardNo(CardUtil.getCardNo());
        return memberDao.save(m);
    }

    @Override
    public void del(Integer id) {
        memberDao.deleteById(id);

    }

    @Override
    public int addAll(List<Member> list) {
        List<Member> memberList =memberDao.saveAll(list);
        return memberList.size();
    }


    @Override
    public void delBatch(List<Member> list) {
       
         memberDao.deleteInBatch(list);
    }

    
}